﻿using System;
using System.Drawing;
using System.Linq;

namespace NeuralLib
{
   

    public class KohonenNeuron
    {
        public Point Coords;
        protected static Random Rand = new Random();
        protected int _inputsCount = 0;

        protected double[] _weights = null;

        private readonly IKohonenMetricFunction _metricFunction;

        public KohonenNeuron(int inputs, Point point, IKohonenMetricFunction metricFunction)
        {
            Coords = point;
            _inputsCount = inputs;
            _metricFunction = metricFunction;
            _weights = new double[inputs];
            SetInitialWeights();
        }

        public static Random Random
        {
            get { return Rand; }
            set
            {
                if (value != null)
                {
                    Rand = value;
                }
            }
        }

        public int InputsCount
        {
            get { return _inputsCount; }
        }
        public double[] Weights
        {
            get { return _weights; }
        }
        public double Compute(double[] inputs)
        {
            return _metricFunction.Function(inputs, _weights);
        }
        public void SetInitialWeights()
        {
            for (var i = 0; i < _inputsCount; i++)
                _weights[i] = Rand.NextDouble() - Rand.NextDouble();
        }

        public string Serialize()
        {
            var weights = _weights.Aggregate("", (current, t) => current + (t + " "));
            return String.Format("{0}:{1}/{2}", Coords.X, Coords.Y, weights);
        }

        
    }
}